//
//  DongDTabBarViewController.m
//  dongdongbook
//
//  Created by yaotd-PC on 2018/2/11.
//  Copyright © 2018年 yaotengdong. All rights reserved.
//

#import "DongDTabBarViewController.h"
#import "DdCustomTabBar.h"
#import "FirstviewController.h"
#import "SecondviewController.h"
#import "ThreeviewController.h"
#import "FourviewController.h"

@interface DongDTabBarViewController () <UITabBarControllerDelegate>

@end

@implementation DongDTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    // Do any additional setup after loading the view.
    //    //  改变标签栏颜色
    //    _tabBarVC.tabBar.barTintColor = [UIColor whiteColor];
    //    //    改变选中的颜色
    //    _tabBarVC.tabBar.tintColor = [UIColor redColor];
    // 利用KVO来使用自定义的tabBar
    [self setValue:[[DdCustomTabBar alloc] init] forKey:@"tabBar"];
    [self addAllChildViewController];
}
#pragma mark - Private Methods

// 添加全部的 childViewcontroller
- (void)addAllChildViewController
{
    FirstviewController *homeVC = [[FirstviewController alloc] init];
    homeVC.view.backgroundColor = [UIColor redColor];
    [self addChildViewController:homeVC title:@"书架" imageNamed:@"书架.png"];
    
    SecondviewController *activityVC = [[SecondviewController alloc] init];
    activityVC.view.backgroundColor = [UIColor yellowColor];
    [self addChildViewController:activityVC title:@"书城" imageNamed:@"书城.png"];
    
    ThreeviewController *findVC = [[ThreeviewController alloc] init];
    findVC.view.backgroundColor = [UIColor blueColor];
    [self addChildViewController:findVC title:@"发现" imageNamed:@"发现.png"];
    
    FourviewController *mineVC = [[FourviewController alloc] init];
    mineVC.view.backgroundColor = [UIColor greenColor];
    [self addChildViewController:mineVC title:@"我的" imageNamed:@"我的.png"];
}
// 添加某个 childViewController
- (void)addChildViewController:(UIViewController *)vc title:(NSString *)title imageNamed:(NSString *)imageNamed
{
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    // 如果同时有navigationbar 和 tabbar的时候最好分别设置它们的title
    vc.navigationItem.title = title;
    nav.tabBarItem.title = title;
    nav.tabBarItem.image = [UIImage imageNamed:imageNamed];
    
    [self addChildViewController:nav];
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NSInteger shouldSelectIndex = [self.viewControllers indexOfObject:viewController];
    if (tabBarController.selectedIndex == shouldSelectIndex) {
        return YES;
    }
    CATransition *animation = [CATransition animation];
    animation.duration = 0.25;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionMoveIn;
//    if (tabBarController.selectedIndex > shouldSelectIndex) {
//        animation.subtype = kCATransitionFromLeft;
//    } else {
        animation.subtype = kCATransitionFromRight;
//    }
    // 与百度上一般文章不一样
    [[[tabBarController valueForKey:@"_viewControllerTransitionView"] layer] addAnimation:animation forKey:@"animation"];
    return YES;
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSInteger index = [self.tabBar.items indexOfObject:item];
        [self animationWithIndex:index];
}
// 动画
- (void)animationWithIndex:(NSInteger) index {
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.08;
    pulse.repeatCount= 1;
    pulse.autoreverses= YES;
    pulse.fromValue= [NSNumber numberWithFloat:0.7];
    pulse.toValue= [NSNumber numberWithFloat:1.3];
    [[tabbarbuttonArray[index] layer]
     addAnimation:pulse forKey:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
